<?php
namespace x;
class G
{
    public static $post=[];
    public static $get=[];
    public static $db=null;
    public static $db1=null;
    public static $uuid = '';
    public static $front_data=[];
    public static $error_data=[];
    public static $domain_config=[];
    public static $exts = [];

    //初始化全局变量
    static public function init(){
        self::$post = self::$post ? self::$post : self::filter($_POST) ;
        self::$get = self::$get ? self::$get : self::filter($_GET);
        //self::$uuid = self::$uuid ? self::$uuid : session_create_id();


        if(!self::$domain_config){
          #域名
          $host = explode(":",$_SERVER['HTTP_HOST']);
          $host = strtolower($host[0]);
          define('DOMAIN',$host);

          #主域名
          $host = explode('.',$host);
          $len = count($host);
          $main_domain = strtolower($host[$len-2].'.'.$host[$len-1]);
          define('MAIN_DOMAIN',$main_domain);

          #域名里的配置
          $rs = env('domain');
          foreach($rs as $k => $v){
            if($k==$main_domain){
              self::$domain_config = $v;
              break;
            }
          }
          if(!self::$domain_config) exit(MAIN_DOMAIN." is not configured");
        }

        #数据库
        if(!self::$db){
            if(self::$domain_config['db']){
                self::$db = Container::get('x\db\driver\Mysqlidb')->init(self::$domain_config['db']);
            }
        }
        if(!self::$db1){
            if(self::$domain_config['db1']){
                self::$db1 = Container::get('x\db\driver\Mysqlidb1')->init(self::$domain_config['db1']);
            }
        }
    }
    //过滤函数
    static public function filter($array= []){
        //return $array;
        foreach($array as $key => $var) {
            if (is_string($var)) {
                $var = trim($var);
                $var = htmlspecialchars($var);
                // if(get_magic_quotes_gpc()){
                //     $var = addslashes($var);
                // }
                $array[$key] = $var;
            }
            if (is_array($var))  {
                $array[$key] = self::filter($var);
            }
        }
        return $array;
    }
}
